package com.sangfor.ssl;

import android.net.NetworkInfo;
import android.text.TextUtils;
import com.sangfor.bugreport.logger.Log;
import com.sangfor.ssl.IConstants;
import com.sangfor.ssl.common.VpnCommon;
import com.sangfor.ssl.easyapp.SangforAuthForward;
import com.sangfor.ssl.l3vpn.service.L3vpnController;
import com.sangfor.ssl.l3vpn.service.VpnServiceManager;
import com.sangfor.ssl.service.line.LineResult;
import com.sangfor.ssl.service.line.LineSelectionTask;
import com.sangfor.ssl.service.line.SelectLineCallback;
import com.sangfor.ssl.service.netmonitor.NetworkMonitor;
import com.sangfor.ssl.service.setting.SettingManager;
import com.sangfor.ssl.service.utils.IGeneral;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;

/* loaded from: classes2.dex */
public class NetworkChangeManager implements NetworkMonitor.NetworkListener, SelectLineCallback {
    private static final int DEFAULT_PORT = 443;
    private static final String TAG = "NetworkChangeManager";

    /* JADX INFO: Access modifiers changed from: private */
    /* loaded from: classes2.dex */
    public static class SingletonHolder {
        private static final NetworkChangeManager INSTANCE = new NetworkChangeManager();

        private SingletonHolder() {
        }
    }

    private NetworkChangeManager() {
    }

    public static final NetworkChangeManager getInstance() {
        return SingletonHolder.INSTANCE;
    }

    public void init() {
        NetworkMonitor.getInstance().registerNetworkMonitor(this);
    }

    public void postLineSelectTask(LineSelectionTask.TaskType taskType) {
        String str = (String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_URL_KEY);
        if (TextUtils.isEmpty(str)) {
            Log.error(TAG, "input url is null, no need line selection");
        } else {
            LineSelectionModel.getInstance().postLineSelectTask(str, taskType, this, null);
        }
    }

    @Override // com.sangfor.ssl.service.netmonitor.NetworkMonitor.NetworkListener
    public void onNetworkChanged(NetworkInfo networkInfo, boolean z) {
        if (NetworkInfo.State.DISCONNECTED == networkInfo.getState() && z) {
            Log.info(TAG, "Network is not available");
        }
        if (networkInfo.isConnected()) {
            Log.info(TAG, "Network is Connected!");
            VpnServiceManager.getInstance().networkChanged(true);
            postLineSelectTask(LineSelectionTask.TaskType.NETWORK_CHANGE);
        }
    }

    @Override // com.sangfor.ssl.service.line.SelectLineCallback
    public void onSelectLineSuccess(LineResult lineResult, boolean z) {
        Log.info(TAG, "Select Line Success. Line Changed:" + z);
        if (!z) {
            Log.info(TAG, "Line is not changed, return.");
            return;
        }
        LineSelectionModel.getInstance().saveSelectInfo((String) SettingManager.getInstance().getGlobalValue(SettingManager.GLOBAL_URL_KEY), lineResult);
        String str = lineResult.vpnAddress;
        try {
            if (!str.startsWith(IGeneral.PROTO_HTTPS_HEAD) && !str.startsWith(IGeneral.PROTO_HTTP_HEAD)) {
                str = String.format(Locale.getDefault(), "https://%s", str);
            }
            URL url = new URL(str);
            SangforAuthForward.getInstance().updateVpnInfo(VpnCommon.ipToLong(url.getHost()), url.getPort() < 0 ? 443 : url.getPort());
            if (SangforAuthManager.getInstance().getVpnMode() == 2 && SangforAuthManager.getInstance().queryStatus() == IConstants.VPNStatus.VPNONLINE) {
                L3vpnController.getInstance().startVPNBusiness();
            }
        } catch (MalformedURLException e) {
            Log.error(TAG, "Malformed URL Error, ", e);
        }
    }

    @Override // com.sangfor.ssl.service.line.SelectLineCallback
    public void onSelectLineFailed(int i) {
        Log.warn(TAG, "Select Line Failed. Code is " + i);
    }
}
